package com.wss.service.user.handler;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wss.common.db.*;
import com.wss.service.user.service.IAuthCodeService;
import com.wss.service.user.service.IUserRolePromiseService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MyUserDetailsService implements UserDetailsService {


    @Autowired
    private IUserRolePromiseService userRolePromiseService;

    @Autowired
    private IAuthCodeService authCodeService;

    @SneakyThrows
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        AuthCode authCode = authCodeService.getOne(new LambdaQueryWrapper<AuthCode>()
                .eq(AuthCode::getUsername, username)
        );
        if(null==authCode){
            authCode=new AuthCode();
            authCode.setCode("01250");
            authCode.setCreatetime(new Date());
            authCode.setEmail("719955043@qq.com");
//            authCode.setId(123);
            authCode.setPassword(getpwd());
            authCode.setTenantId("777");
            authCode.setUsername("wss");
            authCode.setExpiretime(new Date());
            authCodeService.saveOrUpdate(authCode);
//            throw  new WssException("账号或者密码错误！");
        }
//        AuthCodeInfo info = (AuthCodeInfo) authCode;
        AuthCodeInfo info =  bean2bean(authCode,AuthCodeInfo.class);
        List<String> list = userRolePromiseService.list(new LambdaQueryWrapper<UserRolePromise>()
                .eq(UserRolePromise::getUserId, authCode.getId())
        ).stream().map(m -> m.getPromise()).collect(Collectors.toList());
        if(list.size()>0){
            info.setPromises(list);
        }else{
            info.setPromises(new ArrayList<>());
            info.getPromises().add("/api-user/**");
        }
        info.setPassword(info.getPassword());
        return info;
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode("123456");
        System.out.println(encode);
        String encode1 = bCryptPasswordEncoder.encode("123456");
        System.out.println(encode1);
    }

    public static <T> T bean2bean(Object bean1, Class<T> bean2) throws Exception {
        BeanCopier beanCopier = BeanCopier.create(bean1.getClass(), bean2, false);
        Object instance = bean2.newInstance();
        beanCopier.copy(bean1, instance, null);
        return (T)instance;
    }

    public  String getpwd(){
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode("123456");
        return  encode;
    }
}
